/// 某个医院的租借记录汇总列表

var util = require('../../../../utils/util.js');  
var app = getApp()
//常量数据配置
var ConstantData = require('../../../../common/constant_data.js')
//获取应用实例  
Page({
  
  data: {
    
    tabList:[],
    leaseData:'',
    hospitalName:'',
    selStartDate:'',
    selEndDate:'',
    currentTab: 11,  //默认是最后一个TAB
    extBtns:[]
  }, 
  

  
  onLoad: function (options) {
    var that = this;
    var mecId = options.mecId;
    var hospitalName = options.hospitalName;
    var isReal = options.isReal;
    that.data.isReal = isReal;
    this.setData({
      mecId: mecId,
      hospitalName: hospitalName
    });
    var startDate = util.getCurMonth() + "-01";
    var endDate = util.getNextMonth() + "-01";
    that.initExtBtns();
    that.init(startDate,endDate);
    

    console.log(that.data.hospitalList);
   
  },

  /**TAB 页切换
   * 进行数据切换
   */
  swichNav:function(e)
  {
    var that = this;
    console.log(e);
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      });
      var curMonth = e.target.dataset.month;
      var curYear = e.target.dataset.year;
      var startDate = curYear + "-" + (curMonth < 10 ? "0"+ curMonth : curMonth) +"-01";

      var nextMonth = curMonth + 1;
      var nextYear = curYear;
      if(curMonth == 12) {
        nextMonth = 1;
        nextYear = curYear + 1;
      }
      var endDate = nextYear + "-" + (nextMonth < 10 ? "0" + nextMonth : nextMonth) + "-01"; 
      that.init(startDate,endDate);
    }
  },
 
  /** 
   * 点击 ,转到按月来计算明细
   */
  toListMonth: function (e) {

    var that = this;
    console.log(e);
    var position = e.currentTarget.dataset.position;
    var eqtCount = e.currentTarget.dataset.eqtcount;
    wx.navigateTo({    
      url: '../leaselistmonth/leaselistmonth?mecId=' + that.data.mecId + "&hospitalName=" + that.data.hospitalName + "&startDate=" + that.data.selStartDate + "&endDate=" + that.data.selEndDate 
      + "&position=" + position + "&eqtCount=" + eqtCount +"&isReal=" + that.data.isReal,
    });
    
    },

  /**
   * 去扩展的按钮的页面
   */
  toExtBtnPage:function(e) {
    var that = this;
    var url = e.currentTarget.dataset.url;
    var pageNameArr = url.split('/');
    var pageName = pageNameArr[pageNameArr.length - 1];
    wx.navigateTo({
      url: 'ext_btn_page/' + pageName + '/' + pageName + '?mecId=' + that.data.mecId + "&hospitalName=" + that.data.hospitalName 
      + "&isReal=" + isReal ,
    })
  },
  
  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {
   
  },

  /**
   * 初始化扩展的按钮
   */
  initExtBtns:function(){
    var that = this;
    app.executeRequest(function(header){
       wx.request({
         url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_statistic/right/btns',
         header:header,
         data:{  
           position:'leaselist_all',
           isReal: that.data.isReal         
         },
         success:function(res){
           console.log("extBtns",res);
            that.setData({
              extBtns:res.data.btns
            })
         }
       })
    })
  },

  init: function (startDate,endDate) {
    this.setData({
      selStartDate:startDate,
      selEndDate:endDate
    })
    var monthTabList = getMonthList();
    var that = this;    
    app.executeRequest(function (header) {
      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_statistic/lease/total',
        header: header,
        data:{
          startDate:startDate,
          endDate:endDate,
          mecId: that.data.mecId,
          isReal: that.data.isReal
        },
        success: function (res) {
          console.log("dsfsdf---->", res);
          var statisMonth = startDate.substr(5,2);
          var statisYear = startDate.substr(0,4);
          var days = util.getStatisticMonthDays(parseInt(statisYear),parseInt(statisMonth));
          var list = Array();
          var totalData = {"hp_position":"全部" ,"viewDayLeaseCount":true};
          list.push(totalData);
          var tCount = 0;
          var etCount = 0;
          var ptCount = 0;
          for(var i=0;i<res.data.length;i++) {
            var totalCount = parseInt(res.data[i].counts);
            var eqtTotalCount = parseInt(res.data[i].eqt_count);
            var avgCount = totalCount / parseInt(res.data[i].pile_count);
            res.data[i].avg_count = avgCount.toFixed(2);
            if (res.data[i].hp_position != '') {
               list.push(res.data[i]);
               tCount += totalCount;
               etCount = eqtTotalCount;
               ptCount += parseInt(res.data[i].pile_count);
            }
          }
          totalData.viewDayLeaseCount = res.data.length > 0 ? res.data[0].viewDayLeaseCount : true;
          totalData.counts= tCount;
          totalData.eqt_count = etCount;          
          totalData.avg_count = etCount == 0 || days == 0 ? 0 : (tCount/etCount).toFixed(2);
          totalData.pile_count = ptCount;


          that.setData({
            tabList: monthTabList,
            leaseData: list
          })
        }
      })
    });  

  }
})  



//得到月份的列表，目前只默认查看最近6个月的数据
function getMonthList()
{
  var date=new Date();
  var year = date.getFullYear();  //得到当前年份
  var month = date.getMonth() + 1; //得到当前月份，getMonth（）从0-11 ，所以此处需加1

  
  var list=new Array();

  
  if(month >=6)
  {

    for(var i=11;i>=0;i--)
    {
    var obj=new Object();
    obj.year=year;
    obj.month=month-i;
    if (obj.month <= 0) {
        obj.year = year - 1;
        obj.month = (obj.month + 12);
        obj.name = obj.month;
    } else {
        obj.name = obj.month;
    }
    
    list.push(obj);
    }
  }
  else
  {
    for (var i = 11; i >= 0; i--) {
      var obj = new Object();
      obj.year = year ;
      obj.month = month - i;
      if(obj.month<=0)
      {
        
        obj.year = year - 1;
        obj.month=(obj.month+12);
       // obj.name = obj.year + "." + obj.month;
        obj.name =  obj.month;
      }
      else
      {
      obj.name = obj.month;
      }

      list.push(obj);
    }
    
  }

  console.log(list);
  return list;

}

function test() {

 var date=new Date();
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate()  ;

  
  console.log("year="+year);
  console.log("month=" + month);
  console.log("day=" + day);
}